package com.bm.specialvehiclefordispatcher.activity.main;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bm.corelibs.utils.ViewHolder;
import com.bm.corelibs.views.AutoLoadingListView;
import com.bm.specialvehiclefordispatcher.R;
import com.bm.specialvehiclefordispatcher.Tools.StringFormattor;
import com.bm.specialvehiclefordispatcher.activity.BaseFragment;
import com.bm.specialvehiclefordispatcher.activity.order.OrderDetailActivity;
import com.bm.specialvehiclefordispatcher.bean.Order;
import com.bm.specialvehiclefordispatcher.constants.Constant;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;

public class MainOrdersFragment extends BaseFragment implements OnClickListener, OnItemClickListener {

	private AutoLoadingListView lv_orders;
	private LinearLayout ll_empty;
	private TextView tv_empty;
	
	private List<Order> orders = new ArrayList<Order>();
	private MainOrderAdapter adapter = new MainOrderAdapter();
	private OnRefreshListener2<ListView> listener;
	private Runnable refreshCompleted = new Runnable() {
		@Override public void run() {
			lv_orders.OnLoadingFinished();
			lv_orders.onRefreshComplete();
		}
	};
	
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		System.out.println("onCreateView");
		return inflater.inflate(R.layout.fragment_main_orders, container, false);
	}

	@Override
	protected void findViews(View view) {
		lv_orders = (AutoLoadingListView) view.findViewById(R.id.lv_orders);
		ll_empty = (LinearLayout) view.findViewById(R.id.ll_empty);
		tv_empty = (TextView) view.findViewById(R.id.tv_empty);
	}

	@SuppressLint("InflateParams")
	@Override
	protected void init() {
		lv_orders.enablePullDownToRefresh();
		lv_orders.setAdapter(adapter);
		if (adapter.getCount() > 0)
			ll_empty.setVisibility(View.GONE);
	}

	@Override
	protected void addListeners() {
		tv_empty.setOnClickListener(this);
		lv_orders.setOnItemClickListener(this);
		if(listener != null)
			lv_orders.setOnRefreshListener(listener);
	}
	
	public void onRefreshCompleted() {
		handler.post(refreshCompleted);
	}
	
	public AutoLoadingListView getRefreshableView() {
		return lv_orders;
	}
	
	public void setRefreshListener(OnRefreshListener2<ListView> listener) {
		this.listener = listener;
	}
	
	public void resetData() {
		orders.clear();
	}
	
	public void setData(List<Order> orders) {
		if(orders == null || orders.size() <= 0) {
			if(ll_empty != null)
				ll_empty.setVisibility(View.VISIBLE);
		} else {
			if(ll_empty != null)
				ll_empty.setVisibility(View.GONE);
			this.orders.addAll(orders);
			adapter.notifyDataSetChanged();
		}
		
	}
	
	class MainOrderAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return orders.size();
		}

		@Override
		public Object getItem(int position) {
			return orders.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@SuppressLint("InflateParams") @Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null)
				convertView = LayoutInflater.from(mActivity).inflate(R.layout.item_main_orders, null);

			Order order = orders.get(position);

			TextView tv_num = ViewHolder.get(convertView, R.id.tv_num);
			TextView tv_time = ViewHolder.get(convertView, R.id.tv_time);
			TextView tv_style = ViewHolder.get(convertView, R.id.tv_style);
			TextView tv_volume = ViewHolder.get(convertView, R.id.tv_volume);
			TextView tv_condition = ViewHolder.get(convertView, R.id.tv_condition);
			TextView tv_condition_name = ViewHolder.get(convertView, R.id.tv_condition_name);
			TextView tv_status = ViewHolder.get(convertView, R.id.tv_status);
			TextView tv_robcount = ViewHolder.get(convertView, R.id.tv_robcount);
			TextView tv_sum = ViewHolder.get(convertView, R.id.tv_sumprice);
			TextView tv_unitprice = ViewHolder.get(convertView, R.id.tv_unitprice);
			
			tv_num.setText(getResources().getString(R.string.main_hint1) + " " + order.orderNum);
			tv_time.setText(StringFormattor.dropYear(StringFormattor.dropSecond(order.appointmentTime))); 
			tv_style.setText(order.style1 + " (" + order.count + "辆)");
			tv_volume.setText(order.volume + "方");
			tv_sum.setText("￥" + order.sumPrice);
			tv_status.setText(order.getStatusString(order.status));
			tv_status.setTextColor(order.getStatusTextColor(order.status));
			tv_condition.setText(order.condition);
			tv_condition_name.setText(order.conditionName + ":");
			tv_robcount.setText(order.robCount + "");
			tv_unitprice.setText((order.unitPrice + order.changeUnitPrice) + "元/方");
			
			return convertView;
		}

	}

	@Override                                                                                                                                                                                
	public void onClick(View v) {
		startActivity(new Intent(mActivity, FindTrucksActivity.class));
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Order order = (Order) parent.getAdapter().getItem(position);
		Intent intent = new Intent(mActivity, OrderDetailActivity.class);
		intent.putExtra(OrderDetailActivity.ORDER_ID, order.id);
		intent.putExtra(Constant.ORDER_STATUS, order.status);
		startActivity(intent);
	}

}
